<template>
  <div class="app-container">
    <el-form :inline="true" :model="queryParams" label-width="68px" ref="queryForm" v-show="showSearch">
      <el-form-item label="主题" prop="theme">
        <el-input
          @keyup.enter.native="handleQuery"
          clearable
          placeholder="请输入主题"
          size="small"
          v-model="queryParams.theme"
        />
      </el-form-item>
      <el-form-item label="经办人" prop="agent">
        <el-input
          @keyup.enter.native="handleQuery"
          clearable
          placeholder="请输入经办人"
          size="small"
          v-model="queryParams.agent"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select clearable placeholder="请选择状态" size="small" v-model="queryParams.status">
          <el-option label="请选择字典生成" value=""/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery" icon="el-icon-search" size="mini" type="cyan">搜索</el-button>
        <el-button @click="resetQuery" icon="el-icon-refresh" size="mini">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          @click="handleAdd"
          icon="el-icon-plus"
          size="mini"
          type="primary"
          v-hasPermi="['gen:case:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          :disabled="single"
          @click="handleUpdate"
          icon="el-icon-edit"
          size="mini"
          type="success"
          v-hasPermi="['gen:case:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          :disabled="multiple"
          @click="handleDelete"
          icon="el-icon-delete"
          size="mini"
          type="danger"
          v-hasPermi="['gen:case:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          @click="handleExport"
          icon="el-icon-download"
          size="mini"
          type="warning"
          v-hasPermi="['gen:case:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table :data="caseList" @selection-change="handleSelectionChange" v-loading="loading">
      <el-table-column align="center" type="selection" width="55"/>
      <el-table-column align="center" label="id主键" prop="id"/>
      <el-table-column align="center" label="主题" prop="theme"/>
      <el-table-column align="center" label="类型" prop="type"/>
      <el-table-column align="center" label="父级任务" prop="parentTask"/>
      <el-table-column align="center" label="经办人" prop="agent"/>
      <el-table-column align="center" label="优先级" prop="priority"/>
      <el-table-column align="center" label="状态" prop="status"/>
      <el-table-column align="center" label="解决结果" prop="solveResults"/>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button
            @click="handleUpdate(scope.row)"
            icon="el-icon-edit"
            size="mini"
            type="text"
            v-hasPermi="['gen:case:edit']"
          >修改
          </el-button>
          <el-button
            @click="handleDelete(scope.row)"
            icon="el-icon-delete"
            size="mini"
            type="text"
            v-hasPermi="['gen:case:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      :limit.sync="queryParams.pageSize"
      :page.sync="queryParams.pageNum"
      :total="total"
      @pagination="getList"
      v-show="total>0"
    />

    <!-- 添加或修改测试用例对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="500px">
      <el-form :model="form" :rules="rules" label-width="80px" ref="form">

        <el-form-item label="主题" prop="theme">
          <el-input placeholder="请输入主题" v-model="form.theme"/>
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-input placeholder="请输入类型" v-model="form.type"/>
        </el-form-item>
        <el-form-item label="父级任务" prop="parentTask">
          <el-input placeholder="请输入父级任务" v-model="form.parentTask"/>
        </el-form-item>
        <el-form-item label="经办人" prop="agent">
          <el-input placeholder="请输入经办人" v-model="form.agent"/>
        </el-form-item>
        <el-form-item label="优先级" prop="priority">
          <el-input placeholder="请输入优先级" v-model="form.priority"/>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="解决结果" prop="solveResults">
          <el-input placeholder="请输入解决结果" v-model="form.solveResults"/>
        </el-form-item>
        <el-form-item label="模块" prop="module">
          <el-input placeholder="请输入模块" v-model="form.module"/>
        </el-form-item>
        <el-form-item label="期望完成时间" prop="expectedCompletionTime">
          <el-date-picker clearable placeholder="选择期望完成时间" size="small"
                          style="width: 200px"
                          type="date"
                          v-model="form.expectedCompletionTime"
                          value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="原估时间" prop="originalEstimateTime">
          <el-input placeholder="请输入原估时间" v-model="form.originalEstimateTime"/>
        </el-form-item>
        <el-form-item label="标签" prop="label">
          <el-input placeholder="请输入标签" v-model="form.label"/>
        </el-form-item>
      </el-form>
      <div class="dialog-footer" slot="footer">
        <el-button @click="submitForm" type="primary">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {addCase, delCase, getCase, listCase, updateCase} from "@/api/gen/case";

  export default {
    name: "Case",
    components: {},
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 测试用例表格数据
        caseList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          theme: null,
          agent: null,
          status: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询测试用例列表 */
      getList() {
        this.loading = true;
        listCase(this.queryParams).then(response => {
          this.caseList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          delFlag: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          theme: null,
          type: null,
          parentTask: null,
          agent: null,
          priority: null,
          status: "0",
          solveResults: null,
          module: null,
          expectedCompletionTime: null,
          originalEstimateTime: null,
          label: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id);
        this.single = selection.length !== 1;
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加测试用例";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids;
        getCase(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改测试用例";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateCase(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addCase(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除测试用例编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return delCase(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('gen/case/export', {
          ...this.queryParams
        }, `gen_case.xlsx`)
      }
    }
  };
</script>
